;
(function() {
	var config_template = {
		// 合作身份者id，以2088开头的16位纯数字
		"partner": "",
		// 安全检验码，以数字和字母组成的32位字符
		"key": "",
		//RSA private key，App支付必填
		"RSA_private_key": "",
		// 卖家支付宝帐户 必填
		"seller_email": "",
		"host": "",
		// ca证书路径地址，用于curl中ssl校验
		"cacert": __dirname + '/../../cacert.pem',
		// 访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
		"transport": "http",
		// 字符编码格式 目前支持 gbk 或 utf-8
		"input_charset": "utf-8"
	};
	module.exports = function(bus) {
		var alipay_info = bus.info.alipay;
		if (!alipay_info) {
			throwCusError("指定商家未绑定支付宝");
		} else if (!(alipay_info.partner && alipay_info.key && alipay_info.seller_email)) {
			throwCusError("支付宝商家信息不完整");
		}
		var _config = $Object.mix(config_template, {
			host: "http://api.dotnar.com/",
			partner: alipay_info.partner, //"2088511420574362",
			key: alipay_info.key, //"7o9kgtybxdq2elmi7o59su0tpt0kwroo",
			seller_email: alipay_info.seller_email, //"dotnar@hotmail.com",
		});
		/*
		{
			partner:bus.info.alipay.partner,
			key:bus.info.alipay.key
			seller_email:bus.info.alipay.seller_email,
			// host:bus.host,
		}
		 */
		var instance = new Alipay(_config);
		instance.on("create_direct_pay_by_user_trade_finished", pay_online_success);
		instance.on("create_direct_pay_by_user_trade_success", pay_online_success);
		instance.on("wap_trade_create_direct_trade_finished", pay_online_success);
		instance.on("wap_trade_create_direct_trade_success", pay_online_success);
		instance.on("mobile_securitypay_pay_trade_finished", pay_online_success);
		instance.on("mobile_securitypay_pay_trade_success", pay_online_success);
		return instance;
	}

	function pay_online_success(app, pay_ach_id) {
		console.log("在线支付完成:", arguments);
		var pay_ach = PayAchCon.get(pay_ach_id);
		if (!pay_ach) {
			throwCusError("找不到指定支付回调信息")
		} else {
			pay_ach.endPayAch();
			midEvent.emit("ach.online_pay_success", pay_ach_id)
			console.log("支付成功");
		}
	}

	var notify = require("./notify");
	var role = require("./role");
	var Alipay = require('../../my_modules/alipay/index').Alipay;
	var $Object = require("../../core/_include/$.Object");
	var PayAchCon = require("../../core/pay_ach/_con");
	var midEvent = require("../../middleware/index");
}());